package com.tang.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tang.common.entity.community.Article;
import com.tang.common.entity.community.Comment;
import com.tang.common.entity.community.Comment;
import com.tang.community.mapper.ArticleMapper;
import com.tang.community.mapper.CommentMapper;
import com.tang.community.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 唐朝阳
 * @since 2022-05-17
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {


    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public PageInfo<Comment> listComment(String keywords, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        if (keywords == null || "".equals(keywords)) {
            QueryWrapper<Comment> qw = new QueryWrapper<Comment>();
            qw.eq("delete_flag", 1);
            List<Comment> comments = commentMapper.selectList(qw);
            return PageInfo.of(comments);
        }
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<Comment>();
        queryWrapper.eq("delete_flag", 1).like("comment_content", keywords).or().like("article_title", keywords);
        List<Comment> lives = commentMapper.selectList(queryWrapper);
        return PageInfo.of(lives);
    }

    @Override
    public PageInfo<Comment> listMyComment(String userName, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        QueryWrapper<Comment> qw = new QueryWrapper<Comment>();
        qw.eq("delete_flag", 1);
        qw.eq("user_name", userName);
        List<Comment> comments = commentMapper.selectList(qw);
        if(!comments.isEmpty()){
            comments = getTreeComments(comments);
        }
        return PageInfo.of(comments);
    }

    @Override
    public PageInfo<Comment> listArticleComments(Integer articleId, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        QueryWrapper<Comment> qw = new QueryWrapper<Comment>();
        qw.eq("delete_flag", 1);
        qw.eq("article_id", articleId);
        List<Comment> comments = commentMapper.selectList(qw);
        if(!comments.isEmpty()){
            comments = getTreeComments(comments);
        }
        return PageInfo.of(comments);
    }

    public List<Comment> getTreeComments(List<Comment> comments) {
        List<Comment> result = comments.stream().filter(comment -> {
            if (comment.getParentId() == 0) {
                comment.setCommentContent(comment.getUserName() + ":" + comment.getCommentContent());
                return true;
            } else {
                return false;
            }
        }).map(comment -> {
            comment.setChildCommentList(getChildCommentList(comment.getCommentId(), comments));
            return comment;
        }).collect(Collectors.toList());
        return result;
    }

    @Override
    public List<Comment> getNestcomment(String articleTitle) {
        QueryWrapper qw = new QueryWrapper();
        qw.like("article_title", articleTitle);
        List<Comment> comments = commentMapper.selectList(qw);
        List<Comment> result = comments.stream().filter(comment -> {
            if (comment.getParentId() == 0) {
                comment.setCommentContent(comment.getUserName() + ":" + comment.getCommentContent());
                return true;
            } else {
                return false;
            }
        }).map(comment -> {
            comment.setChildCommentList(getChildCommentList(comment.getCommentId(), comments));
            return comment;
        }).collect(Collectors.toList());
        return result;
    }

    private List<Comment> getChildCommentList(Integer commentId, List<Comment> comments) {
        List<Comment> children = comments.stream().filter(childComment -> {
            if (commentId.equals(childComment.getParentId())) {
                childComment.setCommentContent(childComment.getUserName() + ":" + childComment.getCommentContent());
                return true;
            } else {
                return false;
            }
        }).map(comment -> {
            comment.setChildCommentList(getChildCommentList(comment.getCommentId(), comments));
            return comment;
        }).collect(Collectors.toList());
        return children;
    }
}

